package config

import (
	"fmt"
	"gopkg.in/yaml.v3"
	"os"
)

type Config struct {
	DB        Db        `yaml:"db"`
	Redis     Redis     `yaml:"redis"`
	Http      Http      `yaml:"server"`
	AliYunPan AliYunPan `yaml:"aliyunpan"`
}

type Db struct {
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	Database string `yaml:"database"`
	User     string `yaml:"user"`
	Password string `yaml:"password"`
}

type Redis struct {
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	Password string `yaml:"password"`
	DB       int    `yaml:"db"`
}

type Http struct {
	Port   string `yaml:"port"`
	JwtKey string `yaml:"jwtKey"`
}

type AliYunPan struct {
	ClientId     string `yaml:"appid"`
	ClientSecret string `yaml:"secret"`
}

var GlobalConfig Config

func InitConfig() {
	yml, err := os.ReadFile("config/config.yml")
	if err != nil {
		panic("读取配置信息出错:" + err.Error())
	}
	var config Config
	err = yaml.Unmarshal(yml, &config)

	if err != nil {
		panic("配置信息解析错误:" + err.Error())
	}

	GlobalConfig = config
	fmt.Println("config", GlobalConfig)
	fmt.Println("--------配置信息加载成功-------")
}
